package com.honghu.villagepost.dao.manager;

import android.content.Context;

import com.homghu.villagepost.GoodsEntityDao;
import com.homghu.villagepost.StoreEntityDao;
import com.honghu.villagepost.entity.Goods;
import com.honghu.villagepost.entity.dao.GoodsEntity;
import com.honghu.villagepost.entity.dao.StoreEntity;
import com.wuhenzhizao.sku.bean.Sku;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者: liyu on  2017/11/17 0017 19:06
 * 功能描述:数据库管理
 * 备注:
 */
public class DbManager {
    private static DbManager mInstance;

    public static DbManager getInstance() {
        synchronized (DbManager.class) {
            if (mInstance == null) {
                mInstance = new DbManager();
            }
        }
        return mInstance;
    }

    /**
     * Desc:初始化
     * <p>
     * Author: [李豫]
     * Date: 2021-07-15
     *
     * @param context
     * @param userId
     */
    public void init(Context context, String userId) {
        DbHelper.getInstance().init(context, userId);
    }

    /**
     * Desc:退出登录
     * <p>
     * Author: [李豫]
     * Date: 2021-07-15
     */
    public void loginOut() {
        DbHelper.getInstance().clearDb();
    }


    /**
     * Desc:添加商品到购物车
     * <p>
     * Author: [李豫]
     * Date: 2021-07-15
     *
     * @param goods          商品信息
     * @param selectQuantity 加入购物车商品数量
     * @param selectSku      选中的商品规格
     */
    public void addCat(Goods goods, int selectQuantity, Sku selectSku) {
        if (goods != null && selectSku != null) {
            GoodsEntity goodsEntity = goods.covertGoodsEntity();
            goodsEntity.setStockQuantity(selectQuantity);
            goodsEntity.setSkuId(selectSku.getId());
            goodsEntity.setColor(selectSku.getAttributesColor());
            goodsEntity.setSize(selectSku.getAttributesColor());

            GoodsEntity catGoods = getCatGoods(goods.getId(), selectSku.getId());
            if (catGoods != null){
                catGoods.setStockQuantity(catGoods.getStockQuantity() + selectQuantity);
                //商品颜色尺寸一样，就数目累加
                insertCatGoods(catGoods);
            }else {
                //反之不一样，则插入数据库
                insertCatGoods(goodsEntity);
            }

            StoreEntity catShop = getCatShop(goods.getShopId());
            if (catShop == null) {
                catShop = new StoreEntity();
                catShop.setId(goods.getShopId());
                catShop.setShopType(goods.getShopType());
                catShop.setName(goods.getShopName());
                catShop.setLogo(goods.getShopLogo());
            }
            catShop.setUpdateTime(System.currentTimeMillis());
            insertCatShop(catShop);
        }
    }

    /**
     * Desc:获取购物车商铺信息
     * <p>
     * Author: [李豫]
     * Date: 2021-07-15
     *
     * @param shopId
     * @return store entity
     */
    public StoreEntity getCatShop(int shopId) {
        try {
            return DbHelper.getInstance().getDaoSession().getStoreEntityDao().queryBuilder()
                    .where(StoreEntityDao.Properties.Id.eq(shopId)).unique();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * Desc:获取购物车商铺信息
     * <p>
     * Author: [李豫]
     * Date: 2021-07-15
     *
     * @param goodsId
     * @return store entity
     */
    public GoodsEntity getCatGoods(int goodsId, String skuId) {
        try {
            return DbHelper.getInstance().getDaoSession().getGoodsEntityDao().queryBuilder()
                    .where(GoodsEntityDao.Properties.Id.eq(goodsId), GoodsEntityDao.Properties.SkuId.eq(skuId)).unique();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * Desc:获取购物车商铺信息
     * <p>
     * Author: [李豫]
     * Date: 2021-07-15
     *
     * @param shopId
     * @return store entity
     */
    public List<GoodsEntity> getCatGoods(long shopId) {
        try {
            return DbHelper.getInstance().getDaoSession().getGoodsEntityDao().queryBuilder()
                    .where(GoodsEntityDao.Properties.ShopId.eq(shopId)).list();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 插入商铺购物车
     *
     * @param storeEntity
     * @return
     */
    public long insertCatShop(StoreEntity storeEntity) {
        return DbHelper.getInstance().getDaoSession().getStoreEntityDao().insertOrReplace(storeEntity);
    }

    /**
     * 插入商铺购物车
     *
     * @param goodsEntity
     * @return
     */
    public long insertCatGoods(GoodsEntity goodsEntity) {
        return DbHelper.getInstance().getDaoSession().getGoodsEntityDao().insertOrReplace(goodsEntity);
    }

    /**
     * Desc:获取购物车所有商铺信息
     * <p>
     * Author: [李豫]
     * Date: 2021-07-15
     *
     * @return store entity
     */
    public List<StoreEntity> getCatShopList() {
        try {
            List<StoreEntity> list = DbHelper.getInstance().getDaoSession().getStoreEntityDao().queryBuilder().list();
            for (StoreEntity storeEntity: list) {
                List<GoodsEntity> catGoods = getCatGoods(storeEntity.getId());
                storeEntity.setGoodsList(catGoods);
            }
            return list;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * Desc:删除商品
     * <p>
     * Author: [李豫]
     * Date: 2021-07-16
     *
     * @param goodsEntity
     */
    public void deleteGoods(GoodsEntity goodsEntity) {
        DbHelper.getInstance().getDaoSession().getGoodsEntityDao().delete(goodsEntity);
        List<GoodsEntity> catGoods = getCatGoods(goodsEntity.getShopId());
        if (catGoods == null || catGoods.size() <= 0){
            //如果商铺商品为空，则清掉商铺
            DbHelper.getInstance().getDaoSession().getStoreEntityDao().deleteByKey(goodsEntity.getShopId());
        }
    }
}
